import os
import wx.lib.imagebrowser

class AddImageDialog(wx.Dialog):
    def __init__(self, parent, ID, title, size=wx.DefaultSize, pos=wx.DefaultPosition, style=wx.DEFAULT_DIALOG_STYLE, useMetal=False,):
        # Instead of calling wx.Dialog.__init__ we precreate the dialog
        # so we can set an extra style that must be set before
        # creation, and then we create the GUI object using the Create
        # method.
        pre = wx.PreDialog()
        pre.SetExtraStyle(wx.DIALOG_EX_CONTEXTHELP)
        pre.Create(parent, ID, title, pos, size, style)

        # This next step is the most important, it turns this Python
        # object into the real wrapper of the dialog (instead of pre)
        # as far as the wxPython extension is concerned.
        self.PostCreate(pre)

        # This extra style can be set after the UI object has been created.
        if 'wxMac' in wx.PlatformInfo and useMetal:
            self.SetExtraStyle(wx.DIALOG_EX_METAL)


        # Now continue with the normal construction of the dialog
        # contents
        sizer = wx.BoxSizer(wx.VERTICAL)

        box = wx.BoxSizer(wx.HORIZONTAL)

        label = wx.StaticText(self, -1, "Title:")
        label.SetHelpText("Tooltip that is displayed for the image.")
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        self.imageTitleText = wx.TextCtrl(self, -1, "", size=(80,-1))
        self.imageTitleText.SetHelpText("Enter Image Tooltip")
        box.Add(self.imageTitleText, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        box = wx.BoxSizer(wx.HORIZONTAL)

        label = wx.StaticText(self, -1, "Alternate Text:")
        label.SetHelpText("Alternative Text for Accessibility.")
        box.Add(label, 0, wx.ALIGN_CENTRE|wx.ALL, 5)

        self.imageAltText = wx.TextCtrl(self, -1, "", size=(80,-1))
        self.imageAltText.SetHelpText("Enter Textual Represent for the image.")
        box.Add(self.imageAltText, 1, wx.ALIGN_CENTRE|wx.ALL, 5)

        sizer.Add(box, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)
        
        box = wx.BoxSizer(wx.HORIZONTAL)
        label = wx.StaticText(self, -1, "Image File:")
        label.SetHelpText("Actual Image File Location")
        box.Add(label, 0, wx.ALIGN_CENTER | wx.ALL, 5)
        
        self.imageFileText = wx.TextCtrl(self, -1, "", size=(250, -1))
        self.imageFileText.SetHelpText("Location of Image")
        box.Add(self.imageFileText, 1, wx.ALIGN_CENTER | wx.ALL, 5)
        
        btn = wx.Button(self, -1, "Browse...")
        self.Bind(wx.EVT_BUTTON, self.OnButton, btn)
        box.Add(btn, 0, wx.ALIGN_CENTER | wx.ALL, 5)
        sizer.Add(box, 0, wx.GROW | wx.ALIGN_CENTER_VERTICAL | wx.ALL, 5)

        line = wx.StaticLine(self, -1, size=(20,-1), style=wx.LI_HORIZONTAL)
        sizer.Add(line, 0, wx.GROW|wx.ALIGN_CENTER_VERTICAL|wx.RIGHT|wx.TOP, 5)

        btnsizer = wx.StdDialogButtonSizer()
        
        if wx.Platform != "__WXMSW__":
            btn = wx.ContextHelpButton(self)
            btnsizer.AddButton(btn)
        
        btn = wx.Button(self, wx.ID_OK)
        btn.SetHelpText("The OK button completes the dialog")
        btn.SetDefault()
        btnsizer.AddButton(btn)

        btn = wx.Button(self, wx.ID_CANCEL)
        btn.SetHelpText("The Cancel button cancels the dialog.")
        btnsizer.AddButton(btn)
        btnsizer.Realize()

        sizer.Add(btnsizer, 0, wx.ALIGN_CENTER_VERTICAL|wx.ALL, 5)

        self.SetSizer(sizer)
        sizer.Fit(self)
        
    def OnButton(self, event):
        curDir = os.getcwd()
        imageBrowserDialog = wx.lib.imagebrowser.ImageDialog(self, curDir)
        imageBrowserDialog.CenterOnParent()
        if(imageBrowserDialog.ShowModal() == wx.ID_OK):
            self.imageFileText.SetValue(imageBrowserDialog.GetFile())        